jQuery(document).ready(function($) {
    var hasChked=false;
    $("input[name='certnum']").blur(function(){
        if( $(this).val().length==0 ) {
            $("table.detailTbl").hide();
            return;
        }  else if( !hasChked ){
            $("table.detailTbl").hide();
            $("span#msgShow").empty().removeClass('warning alert');
        }
    });
    var postUrl='voucher_check.php';
    var chkNumber=function(){
        var cn = $("input[name='certnum']").val(), salerid=$("input[name='salerid']").val();;
        if(cn=='' || isNaN(cn) ){
            return false;
        }
        var orderStr='';
        $.ajax({
            url:postUrl,
            data:{act:'check',certnum:$("input[name='certnum']").val(),salerid:salerid},
            dataType:'json',
            type:'post',
            beforeSend:function(){
                $("tbody#tbody").empty();
                $("span#msgShow").text('验证中...');
                // $("div.wait").css('display','block');
                $("button#checkSubmitBtn").attr('disabled','disabled');
            },
            success:function(data){
                if(data.errormsg && data.errormsg=='unlogin'){
                   alert('您还未登录，无法执行此操作！');
                    return;
                }
                if(data.errormsg) {
                    $("span#msgShow").text(data.errormsg).removeClass('success alert').addClass('warning alert');
                }
                if( !data.errormsg && data.successMsg=='ok' ) {
                    hasChked = true;
                    $("span#msgShow").text('验证成功！').removeClass('warning alert').addClass('success alert');
                    orderStr+='<tr><td>订单号：'+data.ordernum+'</td></tr><tr><td>流水号：'+data.uuino+'</td></tr>';
                    orderStr+='<tr><td>门票数：'+data.UUtnum+'</td></tr><tr><td>票名称：'+data.UUttitle+'</td></tr>';
                    orderStr+='<tr><td>取票人：'+data.UUordername+'</td></tr><tr><td>手机号：'+data.UUordertel+'</td></tr><tr><td align="center"></td></tr>';
                    $("tbody#tbody").html(orderStr);
                    // $("div.lcon").show();
                }
            },
            error:function(data){
                alert(data);
            },
            complete:function(){
                $("div.wait").css('display','none');
                $("table.detailTbl").show();
                $("button#checkSubmitBtn").removeAttr('disabled');
            }
        });
    };
    $("#checkSubmitBtn").bind('click',chkNumber);
    $("button#at").click(function(){
        $("div#result").empty().hide();
        $("#c_time").show();
    });
    $("button[name='btnCount']").click(function(){
        var t = $(this).attr('data');
        var p = {};
        p.act='count';
        p.t=t;
        p.ticketid=$("select#ticketid option:selected").val();
        p.ttitle=$("select#ticketid option:selected").text();
        p.salerid=salerid;
        if(t==4) {
            if($("input[name='t1']").val() == '' || $("input[name='t2']").val()=='') {
                return;
            }
            p.t1=$("input[name='t1']").val();
            p.t2=$("input[name='t2']").val();
        }
        $.ajax({
            url:postUrl,
            data:p,
            type:'post',
             beforeSend:function(){
                $("div#result").text('请稍等，数据获取中...').show();
             },
             success:function(result){
                $("#c_time").hide();
                $("div#result").html(result);
             }
        })
    });
    $("button#showHis").click(function(){
        location.href='terminal.html?show=1'
    });

    $("select#salerList").bind('change', function(){
        var salerid = $(this).find('option:selected').val();
        $("input#salerid").val(salerid);
    });
    $("#showOrder").bind('click',function(){
        var salerid = $("input#salerid").val(),
            title= $("#salerList").find("option:selected").text();
        title = encodeURIComponent(title);
        //alert(salerid);return;
        if(!salerid) {
            $("#msgShow").text('您要查看哪个产品的订单？请先选择相应的产品吧~').removeClass('success alert').addClass('warning alert');
            return;
        }
        window.location.href='day_orders.html?title='+title+'&salerid='+salerid;
    });
    $("#orderSearch").on('click', function(){
        var salerid = $("input#salerid").val();
        if(!salerid) {
            $("#msgShow").text('您要查看哪个产品的订单？请先选择相应的产品吧~').removeClass('success alert').addClass('warning alert');
            return;
        }
        window.location.href='search.html?salerid='+salerid;
        //search.html
    });
});